package com.example.admin.controller.sys;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.entity.SysDictInfo;
import com.example.admin.service.ISysDictInfoService;
import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.Pager;

/**
 * <p>
 * 字典信息表 前端控制器
 * </p>
 *
 * @author wangkai
 * @since 2019-06-04
 */
@RestController
@RequestMapping("/sys/sysDictInfo")
public class SysDictInfoController {

	@Resource(name = "sysDictInfoService")
	private ISysDictInfoService service;

	/**
	 * 获取参数
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "/page")
	public Pager<SysDictInfo> page(@RequestBody String json) {
		JSONObject jsonObject = JSON.parseObject(json);
		Page<SysDictInfo> page = new Page<SysDictInfo>();
		page.setCurrent(jsonObject.getLongValue("current"));
		page.setSize(jsonObject.getLongValue("size"));
		QueryWrapper<SysDictInfo> queryWrapper = new QueryWrapper<SysDictInfo>();
		queryWrapper.eq("type_id", jsonObject.getIntValue("typeId"));
		return new Pager<SysDictInfo>(service.page(page, queryWrapper));
	}

	/**
	 * 保存
	 * @param info
	 * @return
	 */
	@RequestMapping(value = "/save")
	public AjaxResult save(SysDictInfo info) {
		if (info != null) {
			if (info.insert()) {
				return new AjaxResult(APIState.S00, "保存成功");
			}
		}
		return new AjaxResult(APIState.S01, "保存失败");
	}

	/**
	 * 修改
	 * @param info
	 * @return
	 */
	@RequestMapping(value = "/update")
	public AjaxResult update(SysDictInfo info) {
		if (info != null && info.getId() != null) {
			if (info.insertOrUpdate()) {
				return new AjaxResult(APIState.S00, "保存成功");
			}
		}
		return new AjaxResult(APIState.S01, "保存失败");
	}

	/**
	 * 刪除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/delete")
	public AjaxResult delete(@PathVariable Long id) {
		if (id != null) {
			SysDictInfo info = service.getById(id);
			if (info != null) {
				if (info.deleteById()) {
					return new AjaxResult(APIState.S00, "刪除成功");
				}
			} else {
				return new AjaxResult(APIState.S01, "当前参数不存在请重新刷新列表");
			}
		}
		return new AjaxResult(APIState.S01, "删除失败");
	}

}
